package cn.hy.observer;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class WeatherSubject {
    private List<WeatherObserver> weatherObserverList;
    private WeatherType currentWeather;

    public WeatherSubject( ) {
        weatherObserverList = new ArrayList<>();
        this.currentWeather = WeatherType.SUNNY;
    }


    public void addObserver(WeatherObserver weatherObserver) {
        weatherObserverList.add(weatherObserver);
    }

    public void removeObserver(WeatherObserver weatherObserver) {
        weatherObserverList.remove(weatherObserver);
    }


    public void timePass() {
        WeatherType[] enumValues = WeatherType.values();
        currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
        notifyObservers();
    }

    private void notifyObservers(){
        for (WeatherObserver obs : weatherObserverList) {
            obs.update(currentWeather);
        }
    }


}
